package main

import (
	"fmt"
	"time"

	"github.com/nathan-osman/go-rpigpio"
)

const (
	LEDA_P = 2
	CS_P   = 6
	RST_P  = 4
	RS_P   = 20
	SDA_P  = 21
	SCK_P  = 7
)

var (
	err                         error
	ps                          []*rpi.Pin
	leda, cs, rst, rs, sda, sck *rpi.Pin
)

func init() {
	fmt.Println("scr init begin")
	ps = Set_pin(LEDA_P, CS_P, RST_P, RS_P, SDA_P, SCK_P)
	leda = ps[0] //2
	cs = ps[1]   //3
	rst = ps[2]  //pin 4
	rs = ps[3]   //
	sda = ps[4]
	sck = ps[5]

}
func Set_pin(a ...int) []*rpi.Pin {

	io := make([]*rpi.Pin, len(a))
	for i, v := range a {
		io[i], err = rpi.OpenPin(v, rpi.OUT)
		if err != nil {
			panic(err)
		}

	}
	return io
}

//func write_com1(pin *rpi.Pin, value rpi.Value) {
//	pin.Write(value)
//	return
//}

func Write_pin_int8(pin *rpi.Pin, value uint8) {
	var i uint = 0
	for ; i <= 7; i++ {
		v := (value << i) & 0x80 //		fmt.Printf("%08b %d \n", v, v)//		time.Sleep(time.Millisecond * 100)
		if v == 0 {
			pin.Write(rpi.LOW)
		} else {
			pin.Write(rpi.HIGH)
		}

	}
}

func Write_com1(value uint8) {
	//	uchar i,temp;
	//	temp=com1;
	cs.Write(rpi.LOW) //cs=0;
	rs.Write(rpi.LOW) //rs=0;
	var i uint = 0
	for ; i <= 7; i++ {
		sck.Write(rpi.LOW)       //     sck=0;
		v := (value << i) & 0x80 //		fmt.Printf("%08b %d \n", v, v)//time.Sleep(time.Millisecond * 100)
		if v == 0 {
			sda.Write(rpi.LOW) //		sda=CY;
		} else {
			sda.Write(rpi.HIGH) //		sda=CY;
		}
		sck.Write(rpi.HIGH) //		sck=1;
	}
}
func Write_date(value uint8) {

	cs.Write(rpi.LOW)  //cs=0;
	rs.Write(rpi.HIGH) //rs=1;
	var i uint = 0
	for ; i <= 7; i++ {
		sck.Write(rpi.LOW)       //     sck=0;
		v := (value << i) & 0x80 //		fmt.Printf("%08b %d \n", v, v)//time.Sleep(time.Millisecond * 100)
		if v == 0 {
			sda.Write(rpi.LOW) //		sda=CY;
		} else {
			sda.Write(rpi.HIGH) //		sda=CY;
		}
		sck.Write(rpi.HIGH) //		sck=1;
	}
}

func Writecomdate(page, column uint8, zi16 []uint8) {
	var j, k, nn uint8
	nn = 0
	cs.Write(rpi.LOW) //cs=0;
	for j = 0; j < 2; j++ {
		Write_com1(0xb0 + page + j)
		Write_com1(0x10 + ((column >> 4) & 0x0f))
		Write_com1(0x0f & column)
		for k = 0; k < 15; k++ {
			Write_date(zi16[nn])
			nn++
		}
	}
	cs.Write(rpi.HIGH) //cs=1;
}

func Clear1() {
	var x, y uint8
	for x = 0; x < 9; x++ {
		Write_com1(0xb0 + x)
		Write_com1(0x10)
		Write_com1(0x00)
		for y = 0; y < 132; y++ {
			Write_date(0)
		}
	}
}

func Initlcd() {

	cs.Write(rpi.LOW)
	leda.Write(rpi.HIGH)
	rst.Write(rpi.LOW)                 //	rst=0;
	time.Sleep(time.Millisecond * 100) //	delay(100);
	rst.Write(rpi.HIGH)                //	rst=1;
	time.Sleep(time.Millisecond * 20)  //	delay(20);
	Write_com1(0xe2)                   //	write_com1(0xe2);
	time.Sleep(time.Millisecond * 5)   //	delay(5);
	Write_com1(0x2c)                   //	write_com1(0x2c); /*??1*/
	time.Sleep(time.Millisecond * 5)   //	delay(5);
	Write_com1(0x2e)                   //	write_com1(0x2e); /*??2*/
	time.Sleep(time.Millisecond * 5)   //	delay(5);
	Write_com1(0x2f)                   //	write_com1(0x2f); /*??3*/
	time.Sleep(time.Millisecond * 5)   //	delay(5);
	Write_com1(0x23)                   //	write_com1(0x23); /*????? 0x20~0x27*/
	Write_com1(0x81)                   //	write_com1(0x81); /*????????*/
	Write_com1(0x28)                   //	write_com1(0x28); /*0x1a,?? 0x00~0x3f*/
	Write_com1(0xa2)                   //	write_com1(0xa2); /*1/9 ???(bias)*/
	Write_com1(0xc8)                   //	write_com1(0xc8); /*????*/
	Write_com1(0xa0)                   //	write_com1(0xa0); /*????*/
	Write_com1(0x40)                   //	write_com1(0x40); /*???*/
	Write_com1(0xaf)                   //	write_com1(0xaf); /*???*/
	cs.Write(rpi.HIGH)                 //	cs=1;
	Clear1()                           //	clear1();
}
